package com.blue.excel.model;

import com.blue.excel.annotation.ColField;
import com.blue.excel.annotation.ExtendCols;
import com.blue.excel.annotation.SplitCols;
import com.blue.excel.util.DefaultValueInterceptor;
import com.blue.excel.util.ValueSetInterceptor;

import java.lang.reflect.Field;
import java.util.*;

/**
 * Created by ChenYaHui on 2017/8/1.
 */
public class ColumnField {

    public static Map<String,ValueSetInterceptor> registeredInterceptors = new HashMap<>();

    static {
        registeredInterceptors.put("default",new DefaultValueInterceptor());
    }

    public String title;

    public Field field;

    public ColField colField;

    public ExtendCols extendCols;

    public SplitCols splitCols;

    public List<ColumnField> columnFields = new ArrayList<>();

    public List<String> mapKeys = new ArrayList<>();

    public ValueSetInterceptor valueSetInterceptor;

    public Field keyField;

    public boolean isMap;

    public int getSpan(){
        if(colField != null)
            return 1;
        else if(splitCols != null){
            if(splitCols.itemClass() == Object.class){
                return 2;
            }
            else{
                return columnFields.size();
            }
        }
        else if(extendCols != null){
            if(isMap)
                return mapKeys.size();
            else if(keyField != null)
                return columnFields.size() * mapKeys.size();
            else
                return columnFields.size();
        }
        return 0;
    }

    public int getMapKeyIndex(String key){
        for(int i = 0; i < mapKeys.size(); i++){
            if(mapKeys.get(i).equals(key)){
                return i;
            }
        }
        return -1;
    }


    public ColumnField() {

    }

    public ColumnField(Field field, ColField colField) {
        this.field = field;
        this.colField = colField;
        this.title = colField.title();
        this.valueSetInterceptor = registeredInterceptors.get(colField.valueInterceptorId());
    }

    public void registerInter(String id){
        this.valueSetInterceptor = registeredInterceptors.get(id);
    }
}
